package com.engine.salary.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.salary.biz.*;
import com.engine.salary.config.SalaryElogConfig;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salaryBill.po.SalaryTemplatePO;
import com.engine.salary.entity.salaryacct.po.SalaryAcctRecordPO;
import com.engine.salary.entity.salaryitem.bo.SysSalaryItemBO;
import com.engine.salary.entity.salaryitem.po.SalaryItemPO;
import com.engine.salary.entity.salaryitem.po.SysSalaryItemPO;
import com.engine.salary.entity.salarysob.bo.*;
import com.engine.salary.entity.salarysob.config.SalarySobConfig;
import com.engine.salary.entity.salarysob.dto.SalarySobCycleDTO;
import com.engine.salary.entity.salarysob.param.*;
import com.engine.salary.entity.salarysob.po.*;
import com.engine.salary.entity.taxagent.po.TaxAgentExtRangePO;
import com.engine.salary.entity.taxagent.po.TaxAgentManageRangePO;
import com.engine.salary.enums.OperateTypeEnum;
import com.engine.salary.enums.SalarySystemTypeEnum;
import com.engine.salary.enums.auth.AuthFilterTypeEnum;
import com.engine.salary.enums.salarysob.IncomeCategoryEnum;
import com.engine.salary.exception.SalaryRunTimeException;
import com.engine.salary.mapper.salarysob.SalarySobMapper;
import com.engine.salary.mapper.salarysob.SobTaxLinkMapper;
import com.engine.salary.mapper.taxagent.TaxAgentExtRangeMapper;
import com.engine.salary.service.*;
import com.engine.salary.service.auth.AuthService;
import com.engine.salary.service.auth.AuthServiceImpl;
import com.engine.salary.sys.entity.vo.UploadConfigResponse;
import com.engine.salary.sys.enums.TaxDeclarationFunctionEnum;
import com.engine.salary.sys.service.SalarySysConfService;
import com.engine.salary.sys.service.impl.SalarySysConfServiceImpl;
import com.engine.salary.util.SalaryEntityUtil;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.IdGenerator;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.SalaryPageUtil;
import com.engine.salary.util.valid.RuntimeTypeEnum;
import com.engine.salary.util.valid.ValidUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import weaver.hrm.User;

import java.time.YearMonth;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 薪资账套
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class SalarySobServiceImpl extends Service implements SalarySobService {


    private SalarySobBiz salarySobMapper = new SalarySobBiz();
    private SalarySobRangeBiz salarySobRangeService = new SalarySobRangeBiz();
    private SalarySobEmpFieldBiz empFieldBiz = new SalarySobEmpFieldBiz();
    private SalarySobDefaultItemGroupBiz defaultItemGroupBiz = new SalarySobDefaultItemGroupBiz();
    private SalarySobEmpFieldBiz salarySobEmpFieldService = new SalarySobEmpFieldBiz();
    private SalarySobDefaultItemBiz salarySobDefaultItemService = new SalarySobDefaultItemBiz();
    private SalarySobItemBiz salarySobItemService = new SalarySobItemBiz();
    private SalarySobItemGroupBiz salarySobItemGroupService = new SalarySobItemGroupBiz();

    private SalarySobItemHideBiz salarySobItemHideService = new SalarySobItemHideBiz();

    private SalarySobRangeBiz salarySobRangeBiz = new SalarySobRangeBiz();

//    private SalarySobBackItemMapper getSalarySobBackItemMapper() {
//        return MapperProxyFactory.getProxy(SalarySobBackItemMapper.class);
//    }

    private SalarySobItemService getSalarySobItemService(User user) {
        return ServiceUtil.getService(SalarySobItemServiceImpl.class, user);
    }

    private SalarySobItemGroupService getSalarySobItemGroupService(User user) {
        return ServiceUtil.getService(SalarySobItemGroupServiceImpl.class, user);
    }

    private SalarySobEmpFieldService getSalarySobEmpFieldService(User user) {
        return ServiceUtil.getService(SalarySobEmpFieldServiceImpl.class, user);
    }

    private SalarySobAdjustRuleService getSalarySobAdjustRuleService(User user) {
        return ServiceUtil.getService(SalarySobAdjustRuleServiceImpl.class, user);
    }

    private SalaryFormulaService getSalaryFormulaService(User user) {
        return ServiceUtil.getService(SalaryFormulaServiceImpl.class, user);
    }


    private SalarySobMapper getSalarySobMapper() {
        return MapperProxyFactory.getProxy(SalarySobMapper.class);
    }

    private TaxAgentExtRangeMapper getTaxAgentExtRangeMapper() {
        return MapperProxyFactory.getProxy(TaxAgentExtRangeMapper.class);
    }

    private SobTaxLinkMapper getSobTaxLinkMapper() {
        return MapperProxyFactory.getProxy(SobTaxLinkMapper.class);
    }


    private SalarySobExtRangeService getSalarySobExtRangeService(User user) {
        return ServiceUtil.getService(SalarySobExtRangeServiceImpl.class, user);
    }

    private SalaryItemService getSalaryItemService(User user) {
        return ServiceUtil.getService(SalaryItemServiceImpl.class, user);
    }

    private SysSalaryItemService getSysSalaryItemService(User user) {
        return ServiceUtil.getService(SysSalaryItemServiceImpl.class, user);
    }

    private SalarySobAdjustRuleBiz salarySobAdjustRuleService = new SalarySobAdjustRuleBiz();

    private SalarySobCheckRuleService getSalarySobCheckRuleService(User user) {
        return ServiceUtil.getService(SalarySobCheckRuleServiceImpl.class, user);
    }

    private SalaryAcctRecordService getSalaryAcctRecordService(User user) {
        return ServiceUtil.getService(SalaryAcctRecordServiceImpl.class, user);
    }

    private TaxAgentService getTaxAgentService(User user) {
        return ServiceUtil.getService(TaxAgentServiceImpl.class, user);
    }

    private TaxAgentAdminService getTaxAgentAdminService(User user) {
        return ServiceUtil.getService(TaxAgentAdminServiceImpl.class, user);
    }

    private TaxAgentManageRangeService getTaxAgentManageRangeService(User user) {
        return ServiceUtil.getService(TaxAgentManageRangeServiceImpl.class, user);
    }

    private SalarySobBackItemService getSalarySobBackItemService(User user) {
        return ServiceUtil.getService(SalarySobBackItemServiceImpl.class, user);
    }

    private SalaryTemplateService getSalaryTemplateService(User user) {
        return ServiceUtil.getService(SalaryTemplateServiceImpl.class, user);
    }

    private SalarySysConfService getSalarySysConfService(User user) {
        return ServiceUtil.getService(SalarySysConfServiceImpl.class, user);
    }

    public AuthService getAuthService(User user) {
        return ServiceUtil.getService(AuthServiceImpl.class, user);
    }

    private SalaryApprovalRuleService getSalaryApprovalRuleService(User user) {
        return ServiceUtil.getService(SalaryApprovalRuleServiceImpl.class, user);
    }

    @Override
    public SalarySobPO getById(Long id) {
        handleHistory();
        return getSalarySobMapper().getById(id);
    }


    @Override
    public List<SalarySobPO> listByIds(Collection<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return getSalarySobMapper().listSome(SalarySobPO.builder().ids(ids).build());
    }

    @Override
    public List<SalarySobPO> listByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return Collections.emptyList();
        }
        List<SalarySobPO> salarySobPOS = salarySobMapper.listByName(SalarySobPO.builder().name(name).build());
        return salarySobPOS;
    }

    @Override
    public List<SalarySobPO> listByNameLike(String nameLike) {
        if (StringUtils.isEmpty(nameLike)) {
            return Collections.emptyList();
        }
        List<SalarySobPO> salarySobPOS = salarySobMapper.listByParam(SalarySobPO.builder().name(nameLike).build());
        return getAuthService(user).auth(salarySobPOS, AuthFilterTypeEnum.DATA_OPT, SalarySobPO.class);
    }

    @Override
    public List<SalarySobPO> listByDisable(Integer disable) {
        List<SalarySobPO> salarySobPOS = salarySobMapper.listSome(SalarySobPO.builder().disable(disable).build());
        return getAuthService(user).auth(salarySobPOS, AuthFilterTypeEnum.DATA_OPT, SalarySobPO.class);
    }

    @Override
    public List<SalarySobPO> listAll() {
        return salarySobMapper.listSome(SalarySobPO.builder().build());
    }

    @Override
    public List<SalarySobPO> listAuth(SalarySobQueryParam param) {
        // 开启分权后需要过滤薪资账套
        String name = param.getName();
        // 查询所有的薪资账套
        SalarySobPO build = SalarySobPO.builder().build();
        if (StringUtils.isNotBlank(name)) {
            build.setName(name);
        }
        if (param.getTaxAgentId() != null) {
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().taxAgentId(param.getTaxAgentId()).build());
            build.setIds(sobTaxLinkPOS.stream().map(SobTaxLinkPO::getTaxAgentId).collect(Collectors.toList()));
        }

        if(CollectionUtil.isNotEmpty(param.getTaxAgentIds())){
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().taxAgentIds(param.getTaxAgentIds()).build());
            build.setIds(sobTaxLinkPOS.stream().map(SobTaxLinkPO::getTaxAgentId).collect(Collectors.toList()));
        }

        List<SalarySobPO> list = getSalarySobMapper().listSome(build);
        return getAuthService(user).auth(list, param.getFilterType(), SalarySobPO.class);
    }

    @Override
    public List<SalarySobPO> listByAdmin() {
        List<SalarySobPO> salarySobPOS = salarySobMapper.listSome(SalarySobPO.builder().build());
        return getAuthService(user).auth(salarySobPOS, AuthFilterTypeEnum.QUERY_DATA, SalarySobPO.class);
    }

    @Override
    public List<SalarySobPO> listByTaxAgentId(Long taxAgentId) {
        return getSalarySobMapper().listByTaxAgentId(taxAgentId);
    }

    @Override
    public List<SalarySobPO> listByIncomeCategory(IncomeCategoryEnum incomeCategory) {
        return salarySobMapper.listSome(SalarySobPO.builder().incomeCategory(incomeCategory.getValue()).build());
    }

    @Override
    public PageInfo<SalarySobPO> listPageByParam(SalarySobListQueryParam queryParam) {
        // 开启分权后需要过滤薪资账套
        String name = queryParam.getName();
        // 查询所有的薪资账套
        SalarySobPO build = SalarySobPO.builder().build();
        if (StringUtils.isNotBlank(name)) {
            build.setName(name);
        }
        if (queryParam.getTaxAgentId() != null) {
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().taxAgentId(queryParam.getTaxAgentId()).build());
            build.setIds(sobTaxLinkPOS.stream().map(SobTaxLinkPO::getSobId).collect(Collectors.toList()));
        }

        List<SalarySobPO> list = getSalarySobMapper().listSome(build);

        list = getAuthService(user).auth(list, AuthFilterTypeEnum.DATA_OPT, SalarySobPO.class);

        return SalaryPageUtil.buildPage(queryParam.getCurrent(), queryParam.getPageSize(), list, SalarySobPO.class);
    }

    @Override
    public List<SalarySobPO> listByParam(SalarySobListQueryParam queryParam) {

        String name = queryParam.getName();
        // 查询所有的薪资账套
        SalarySobPO build = SalarySobPO.builder().build();
        if (StringUtils.isNotBlank(name)) {
            build.setName(name);
        }
        if (queryParam.getTaxAgentId() != null) {
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().taxAgentId(queryParam.getTaxAgentId()).build());
            build.setIds(sobTaxLinkPOS.stream().map(SobTaxLinkPO::getTaxAgentId).collect(Collectors.toList()));
        }

        List<SalarySobPO> list = salarySobMapper.listSome(build);

        return getAuthService(user).auth(list, AuthFilterTypeEnum.DATA_OPT, SalarySobPO.class);
    }

    @Override
    public Long save(SalarySobBasicSaveParam saveParam) {

        ValidUtil.doValidator(saveParam);

        // 薪资账套的名称不能重复
        List<SalarySobPO> salarySobPOS = listByName(saveParam.getName());
        if (CollectionUtils.isNotEmpty(salarySobPOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98403, "薪资账套名称已存在"));
        }

        // 保存参数转换成薪资账套po
        SalarySobPO salarySobPO = SalarySobBO.convert2PO(saveParam, (long) user.getUID());
        // 保存薪资账套
        salarySobMapper.insert(salarySobPO);
        // 记录日志
        LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        loggerContext.setTargetId(String.valueOf(salarySobPO.getId()));
        loggerContext.setTargetName(salarySobPO.getName());
        loggerContext.setOperateType(OperateTypeEnum.ADD.getValue());
        loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "新建薪资账套"));
        loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "新建薪资账套") + ": " + salarySobPO.getName());
        loggerContext.setNewValues(salarySobPO);
        SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);

        //保存个税扣缴义务人联系
        saveSobTaxLink(saveParam.getTaxAgentIds(), salarySobPO);
        // 新建薪资账套时，保存默认的员工信息字段
        saveDefaultEmpField(salarySobPO);
        // 新建薪资账套时，保存默认的薪资项目
        saveDefaultItem(salarySobPO);
        // 新建薪资账套时，保存默认的关联人员范围及从范围中排除
        saveDefaultEmployeeRange(salarySobPO);
        // 返回薪资账套的主键id
        return salarySobPO.getId();
    }

    private void saveSobTaxLink(List<Long> taxAgentIds, SalarySobPO salarySobPO) {
        getSobTaxLinkMapper().deleteBySobId(salarySobPO.getId());
        for (Long taxAgentId : taxAgentIds) {
            SobTaxLinkPO taxLinkPO = SobTaxLinkPO.builder()
                    .id(IdGenerator.generate())
                    .taxAgentId(taxAgentId)
                    .sobId(salarySobPO.getId())
                    .creator((long) user.getUID())
                    .createTime(new Date())
                    .updateTime(new Date())
                    .deleteType(0)
                    .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                    .build();
            getSobTaxLinkMapper().insertIgnoreNull(taxLinkPO);
        }
    }


    /**
     * 新建薪资账套时，保存默认的员工信息字段
     *
     * @param salarySobPO 薪资账套
     */
    private void saveDefaultEmpField(SalarySobPO salarySobPO) {
        // 查询薪资账套默认的员工信息字段
        List<SalarySobDefaultEmpFieldPO> salarySobDefaultEmpFieldPOS = empFieldBiz.listDefaultEmpField();
        // 转换成薪资账套员工信息字段po
        List<SalarySobEmpFieldPO> salarySobEmpFieldPOS = SalarySobItemBO.convert2EmpFieldPO(salarySobPO.getId(), salarySobDefaultEmpFieldPOS, (long) user.getUID());
        // 保存
        empFieldBiz.batchInsert(salarySobEmpFieldPOS);
    }

    /**
     * 新建薪资账套时，保存默认的薪资项目
     * 1、薪资账套默认引用的系统薪资项目
     * 2、自定义薪资项目中开启了"默认使用"的薪资项目
     *
     * @param salarySobPO 薪资账套
     */
    private void saveDefaultItem(SalarySobPO salarySobPO) {
        // 1、根据薪资类型查询薪资账套默认使用的薪资项目分类以及薪资项目
        IncomeCategoryEnum incomeCategoryEnum = IncomeCategoryEnum.parseByValue(salarySobPO.getIncomeCategory());
        List<SalarySobDefaultItemGroupPO> salarySobDefaultItemGroupPOS = defaultItemGroupBiz.listByIncomeCategory(incomeCategoryEnum);
        List<SalarySobDefaultItemPO> salarySobDefaultItemPOS = salarySobDefaultItemService.listByIncomeCategory(SalarySobDefaultItemPO.builder().incomeCategory(incomeCategoryEnum.getValue()).build());

        //是否需要系统算税，不需要则去除系统项目
        TaxDeclarationFunctionEnum taxDeclarationFunctionEnum = getSalarySysConfService(user).getTaxDeclaration();
        if (taxDeclarationFunctionEnum == TaxDeclarationFunctionEnum.CLOSURE) {
            salarySobDefaultItemGroupPOS = new ArrayList<>();
            salarySobDefaultItemPOS = new ArrayList<>();
        }

        // 2、薪资账套默认引用的系统薪资项目如果没有添加到薪资项目中，这里要给添加保存
        // 2.1、查询已经添加到薪资项目中的系统薪资项目
        Set<Long> defaultSysSalaryItemIds = SalaryEntityUtil.properties(salarySobDefaultItemPOS, SalarySobDefaultItemPO::getSysSalaryItemId);
        // 薪资账套的默认的回算薪资项目
        Set<Long> SalarySobBackItemIds = SalaryEntityUtil.properties(SalarySobBackItemBO.getDefault(), SalarySobDefaultBackItemPO::getSysSalaryItemId);
        defaultSysSalaryItemIds.addAll(SalarySobBackItemIds);

        List<SalaryItemPO> salaryItemPOS = getSalaryItemService(user).listBySysSalaryItemIds(defaultSysSalaryItemIds);
        Set<Long> sysSalaryItemIds = SalaryEntityUtil.properties(salaryItemPOS, SalaryItemPO::getSysSalaryItemId);
        // 2.2、需要添加进薪资项目中的系统薪资项目id
        Set<Long> needAddSysSalaryItemIds = salarySobDefaultItemPOS.stream()
                .map(SalarySobDefaultItemPO::getSysSalaryItemId)
                .filter(sysSalaryItemId -> !sysSalaryItemIds.contains(sysSalaryItemId))
                .collect(Collectors.toSet());
        // 需要添加进薪资项目中的回算薪资项目
        needAddSysSalaryItemIds.addAll(SalarySobBackItemIds.stream().filter(salarySobBackItemId -> !sysSalaryItemIds.contains(salarySobBackItemId)).collect(Collectors.toList()));
        List<SysSalaryItemPO> needAddSysSalaryItemPOS = getSysSalaryItemService(user).listByIds(needAddSysSalaryItemIds);
        // 2.3、需要保存的薪资项目
        List<SalaryItemPO> needInsertSalaryItemPOS = SysSalaryItemBO.convert2SalaryItemPO(needAddSysSalaryItemPOS, (long) user.getUID());
        if (CollectionUtils.isNotEmpty(needInsertSalaryItemPOS)) {
            getSalaryItemService(user).batchSave(needInsertSalaryItemPOS);
        }

        // 3、查询开启了"默认使用"的自定义薪资项目
        List<SalaryItemPO> useDefaultSalaryItemPOS = getSalaryItemService(user).listBySystemTypeAndUseDefault(SalarySystemTypeEnum.CUSTOM, NumberUtils.INTEGER_ONE);

        // 4、默认引用的薪资项目（=薪资账套默认引用的系统薪资项目(已添加进薪资项目中的) + 薪资账套默认引用的系统薪资项目(待添加进薪资项目中的) + 自定义薪资项目中开启了"默认使用"的薪资项目）
        List<SalaryItemPO> defaultSalaryItemPOS = Lists.newArrayListWithExpectedSize(salaryItemPOS.size() + needInsertSalaryItemPOS.size() + useDefaultSalaryItemPOS.size());
        defaultSalaryItemPOS.addAll(salaryItemPOS);
        defaultSalaryItemPOS.addAll(needInsertSalaryItemPOS);
        defaultSalaryItemPOS.addAll(useDefaultSalaryItemPOS);

        // 5、保存
        SalarySobItemBO.Result result = SalarySobItemBO.initSalarySobItem(salarySobPO.getId(), salarySobDefaultItemGroupPOS, salarySobDefaultItemPOS, defaultSalaryItemPOS, (long) user.getUID(), SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY);
        if (CollectionUtils.isNotEmpty(result.getNeedInsertSalarySobItems())) {

            List<SalarySobItemPO> list = new ArrayList<>(result.getNeedInsertSalarySobItems().stream().collect(Collectors.toMap(SalarySobItemPO::getSalaryItemId, Function.identity(), (oldValue, newValue) -> oldValue)).values());

            salarySobItemService.batchInsert(list);
        }
        if (CollectionUtils.isNotEmpty(result.getNeedInsertSalarySobItemGroups())) {
            salarySobItemGroupService.batchInsert(result.getNeedInsertSalarySobItemGroups());
        }
        if (CollectionUtils.isNotEmpty(result.getNeedInsertSalarySobBackItems())) {
            getSalarySobBackItemService(user).batchInsert(result.getNeedInsertSalarySobBackItems());
        }
    }

    /**
     * @return void
     * @description 新建薪资账套时，保存默认的关联人员范围及从范围中排除
     * @author Harryxzy
     * @date 2022/10/9 15:30
     */
    public void saveDefaultEmployeeRange(SalarySobPO salarySobPO) {
        Long sobId = salarySobPO.getId();
        salarySobPO = getById(sobId);

        salarySobPO.getTaxAgentIds().forEach(taxAgentId -> {
            // 获取人员范围列表
            List<TaxAgentManageRangePO> includeList = getTaxAgentManageRangeService(user).listByTaxAgentIdAndIncludeType(taxAgentId, NumberUtils.INTEGER_ONE);
            includeList.forEach(item -> {
                item.setEmployeeStatus(parseJson2ValueStr(item.getEmployeeStatus()));
            });
            // 获取从范围中排除
            List<TaxAgentManageRangePO> excludeList = getTaxAgentManageRangeService(user).listByTaxAgentIdAndIncludeType(taxAgentId, NumberUtils.INTEGER_ZERO);
            excludeList.forEach(item -> {
                item.setEmployeeStatus(parseJson2ValueStr(item.getEmployeeStatus()));
            });
            // 将TaxAgentManageRangeListDTO转换为SalarySobRangePO
            List<SalarySobRangePO> rangeList = convert2SalarySobRangePO(sobId, includeList, excludeList);
            // 保存SalarySobRangePO
            if (CollectionUtils.isNotEmpty(rangeList)) {
                salarySobRangeBiz.batchInsert(rangeList);
            }

            //非系统人员
            List<TaxAgentExtRangePO> taxAgentExtRangePOS = getTaxAgentExtRangeMapper().listSome(TaxAgentExtRangePO.builder().taxAgentId(taxAgentId).build());
            taxAgentExtRangePOS.forEach(po -> {
                SalarySobRangeExtSaveParam build = SalarySobRangeExtSaveParam.builder().salarySobId(sobId).targetIds(Collections.singletonList(po.getTargetId())).targetType(1L).build();
                getSalarySobExtRangeService(user).saveExtRange(build);
            });
        });


    }


    /**
     * @return List<SalarySobRangePO>
     * @description 将TaxAgentManageRangeListDTO转换为SalarySobRangePO
     * @author Harryxzy
     * @date 2022/10/9 16:06
     */
    private List<SalarySobRangePO> convert2SalarySobRangePO(Long salarySobID, List<TaxAgentManageRangePO> includeList, List<TaxAgentManageRangePO> excludeList) {
        Date now = new Date();
        ArrayList<SalarySobRangePO> result = new ArrayList<SalarySobRangePO>();
        // 关联人员范围
        includeList.stream().forEach(item -> {
            SalarySobRangePO salarySobRangePO = SalarySobRangePO.builder()
                    .salarySobId(salarySobID)
                    .targetType(item.getTargetType())
                    .targetId(item.getTargetId())
                    .target(item.getTarget())
                    .employeeStatuses(item.getEmployeeStatus())
                    .includeType(1)
                    .creator(Long.valueOf(user.getUID()))
                    .createTime(now)
                    .updateTime(now)
                    .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                    .deleteType(0)
                    .build();
            result.add(salarySobRangePO);
        });
        // 从范围中排除
        excludeList.stream().forEach(item -> {
            SalarySobRangePO salarySobRangePO = SalarySobRangePO.builder()
                    .salarySobId(salarySobID)
                    .targetType(item.getTargetType())
                    .targetId(item.getTargetId())
                    .target(item.getTarget())
                    .employeeStatuses(item.getEmployeeStatus())
                    .includeType(0)
                    .creator(Long.valueOf(user.getUID()))
                    .createTime(now)
                    .updateTime(now)
                    .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                    .deleteType(0)
                    .build();
            result.add(salarySobRangePO);
        });
        return result;
    }


    /**
     * ["0","1","2","3","4","5","6"]  ->  0,1,2,3,4,5,6
     * @param employeeStatus
     * @return
     */
    private String parseJson2ValueStr(String employeeStatus) {
        return  employeeStatus.replace("[","").replace("]","").replaceAll("\"", "");
    }


    @Override
    public Long update(SalarySobBasicSaveParam saveParam) {

        ValidUtil.doValidator(saveParam, RuntimeTypeEnum.UPDATE);

        // 查询薪资账套
        SalarySobPO salarySobPO = getById(saveParam.getId());
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        // 薪资账套的名称不能重复
        List<SalarySobPO> salarySobPOS = listByName(saveParam.getName());
        boolean duplicateName = salarySobPOS.stream().anyMatch(e -> !Objects.equals(e.getId(), saveParam.getId()));
        if (duplicateName) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98403, "薪资账套名称已存在"));
        }
        // 更新薪资账套
        SalarySobPO newSalarySobPO = new SalarySobPO();
        BeanUtils.copyProperties(salarySobPO, newSalarySobPO);
        newSalarySobPO.setId(saveParam.getId())
                .setName(saveParam.getName())
                .setTaxAgentId(saveParam.getTaxAgentIds().get(0))
                .setIncomeCategory(saveParam.getTaxableItems())
                .setSalaryCycleType(saveParam.getSalaryCycleType())
                .setSalaryCycleFromDay(saveParam.getSalaryCycleFromDay())
                .setTaxCycleType(saveParam.getTaxCycleType())
                .setAttendCycleType(saveParam.getAttendCycleType())
                .setAttendCycleFromDay(saveParam.getAttendCycleFromDay())
                .setSocialSecurityCycleType(saveParam.getSocialSecurityCycleType())
                .setDescription(saveParam.getDescription())
                .setUpdateTime(new Date());
        salarySobMapper.updateById(newSalarySobPO);

        //保存个税扣缴义务人联系
        saveSobTaxLink(saveParam.getTaxAgentIds(), salarySobPO);

        // 记录日志
        SalarySobPO salarySobPO4log = getSalarySobMapper().getById(newSalarySobPO.getId());
        LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        loggerContext.setTargetId(String.valueOf(newSalarySobPO.getId()));
        loggerContext.setTargetName(newSalarySobPO.getName());
        loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
        loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "编辑薪资账套基础设置"));
        loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "编辑薪资账套基础设置"));
        loggerContext.setOldValues(salarySobPO);
        loggerContext.setNewValues(salarySobPO4log);
        SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);
        // 返回薪资账套的主键id
        return salarySobPO.getId();
    }

    @Override
    public void updateDisable(SalarySobDisableParam disableParam) {
        // 查询薪资账套
        SalarySobPO salarySobPO = getById(disableParam.getId());
        SalarySobPO oldSalarySobPO = new SalarySobPO();
        BeanUtils.copyProperties(salarySobPO, oldSalarySobPO);
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        // 启用/禁用 薪资账套
        salarySobPO.setDisable(disableParam.getDisable());
        salarySobPO.setUpdateTime(new Date());
        salarySobMapper.updateById(salarySobPO);
        // 记录日志
        String operateTypeName = Objects.equals(disableParam.getDisable(), NumberUtils.INTEGER_ONE) ?
                SalaryI18nUtil.getI18nLabel(0, "禁用薪资账套") : SalaryI18nUtil.getI18nLabel(0, "启用薪资账套");
        LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        loggerContext.setTargetId("" + salarySobPO.getId());
        loggerContext.setTargetName(salarySobPO.getName());
        loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
        loggerContext.setOperateTypeName(operateTypeName);
        loggerContext.setOperatedesc(operateTypeName + ": " + salarySobPO.getName());
        loggerContext.setOldValues(oldSalarySobPO);
        loggerContext.setNewValues(salarySobPO);
        SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);
    }

    @Override
    public void deleteByIds(Collection<Long> ids) {

        //分权
        Boolean isOpenDevolution = getTaxAgentService(user).isOpenDevolution();
        if (isOpenDevolution) {
            List<SalarySobPO> salarySobPOS = listByAdmin();
            Set<Long> salarySobIds = SalaryEntityUtil.properties(salarySobPOS, SalarySobPO::getId);
            Optional<Long> first = ids.stream().filter(f -> !salarySobIds.contains(f)).findFirst();
            if (first.isPresent()) {
                throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "无权限删除"));
            }
        }


        // 查询薪资账套
        List<SalarySobPO> salarySobPOS = listByIds(ids);
        if (CollectionUtils.isEmpty(salarySobPOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        ids = SalaryEntityUtil.properties(salarySobPOS, SalarySobPO::getId);
        // 根据薪资账套id查询薪资核算记录，存在薪资核算记录就不能删除账套了
        List<SalaryAcctRecordPO> salaryAcctRecordPOS = getSalaryAcctRecordService(user).listBySalarySobIds(ids);
        if (CollectionUtils.isNotEmpty(salaryAcctRecordPOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(99713, "账套已用于薪资核算，不能删除"));
        }

        // 根据薪资账套id查询工资单模板，存在该账套下的工资单模板就不能删除账套了
        List<SalaryTemplatePO> salaryTemplatePOS = getSalaryTemplateService(user).getBySalarySobIds(ids);
        if (CollectionUtils.isNotEmpty(salaryTemplatePOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(99713, "账套存在工资单模板，不能删除，请先删除该账套所对应的工资单模板"));
        }

        //关联其他的删除
        // 删除薪资账套
        salarySobMapper.deleteByIds(ids);
        // 删除薪资账套的人员范围
        salarySobRangeService.deleteBySalarySobIds(ids);
        // 删除薪资账套的人员信息字段
        salarySobEmpFieldService.deleteBySalarySobIds(ids);
        // 删除薪资账套的薪资项目
        salarySobItemService.deleteBySalarySobIds(ids);
        // 删除薪资账套的薪资项目分类
        salarySobItemGroupService.deleteBySalarySobIds(ids);
        // 删除薪资账套的薪资项目隐藏信息
        salarySobItemHideService.deleteBySalarySobIds(ids);
        // 删除薪资账套的调薪计薪规则
        salarySobAdjustRuleService.deleteBySalarySobIds(ids);
        // 删除薪资账套的回算薪资项目
        getSalarySobBackItemService(user).deleteBySalarySobIds(ids);
        // 删除薪资账套的校验规则
        getSalarySobCheckRuleService(user).deleteBySalarySobIds(ids);
        // 删除薪资审批规则
        getSalaryApprovalRuleService(user).deleteBySalarySobIds(ids);
       // 记录日志
        salarySobPOS.forEach(salarySobPO -> {
            LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
            loggerContext.setUser(user);
            loggerContext.setTargetId("" + salarySobPO.getId());
            loggerContext.setTargetName(salarySobPO.getName());
            loggerContext.setOperateType(OperateTypeEnum.DELETE.getValue());
            loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(0, "删除薪资账套"));
            loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(0, "删除薪资账套"));
            loggerContext.setOldValues(salarySobPO);
            SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);
        });
    }

    @Override
    public void duplicate(SalarySobDuplicateParam duplicateParam) {
        ValidUtil.doValidator(duplicateParam);

        // 查询薪资账套
        SalarySobPO salarySobPO = getById(duplicateParam.getId());
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        // 薪资账套名称不能重复
        List<SalarySobPO> salarySobPOS = listByName(duplicateParam.getName());
        if (CollectionUtils.isNotEmpty(salarySobPOS)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98403, "薪资账套名称已存在"));
        }

        // 查询薪资账套的员工信息字段
        List<SalarySobEmpFieldPO> salarySobEmpFieldPOS = salarySobEmpFieldService.listSome(SalarySobEmpFieldPO.builder().salarySobId(duplicateParam.getId()).build());
        // 查询薪资账套的薪资项目副本
        List<SalarySobItemPO> salarySobItemPOS = salarySobItemService.listSome(SalarySobItemPO.builder().salarySobId(duplicateParam.getId()).build());
        // 薪资项目副本去重
        salarySobItemPOS = salarySobItemPOS.stream().filter(SalaryEntityUtil.distinctByKey(PO -> PO.getSalarySobId() + "-" + PO.getSalaryItemId())).collect(Collectors.toList());
        // 查询薪资账套的薪资项目分类
        List<SalarySobItemGroupPO> salarySobItemGroupPOS = salarySobItemGroupService.listSome(SalarySobItemGroupPO.builder().salarySobId(duplicateParam.getId()).build());
        // 查询薪资账套的调薪计薪规则
        List<SalarySobAdjustRulePO> salarySobAdjustRulePOS = salarySobAdjustRuleService.listBySalarySobId(duplicateParam.getId());
        // 查询薪资账套的校验规则
        List<SalarySobCheckRulePO> salarySobCheckRulePOS = getSalarySobCheckRuleService(user).listBySalarySobId(duplicateParam.getId());
        // 查询薪资账套的回算薪资项目
        List<SalarySobBackItemPO> salarySobBackItemPOS = getSalarySobBackItemService(user).listBySalarySobId(duplicateParam.getId());


        // 复制薪资账套的基础设置
        SalarySobPO newSalarySob = SalarySobPO.builder()
                .name(duplicateParam.getName())
                .taxAgentId(duplicateParam.getTaxAgentIds().get(0))
                .incomeCategory(salarySobPO.getIncomeCategory())
                .salaryCycleType(salarySobPO.getSalaryCycleType())
                .salaryCycleFromDay(salarySobPO.getSalaryCycleFromDay())
                .taxCycleType(salarySobPO.getTaxCycleType())
                .attendCycleType(salarySobPO.getAttendCycleType())
                .attendCycleFromDay(salarySobPO.getAttendCycleFromDay())
                .socialSecurityCycleType(salarySobPO.getSocialSecurityCycleType())
                .disable(salarySobPO.getDisable())
                .creator((long) user.getUID())
                .createTime(new Date())
                .updateTime(new Date())
                .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                .deleteType(NumberUtils.INTEGER_ZERO)
                .build();
        salarySobMapper.insert(newSalarySob);


        saveSobTaxLink(duplicateParam.getTaxAgentIds(), newSalarySob);
        // 复制
        SalarySobDuplicateBO salarySobDuplicateBO = new SalarySobDuplicateBO(newSalarySob, salarySobEmpFieldPOS, salarySobItemPOS,
                salarySobItemGroupPOS, salarySobAdjustRulePOS, salarySobCheckRulePOS, salarySobBackItemPOS);
        SalarySobDuplicateBO.Result result = salarySobDuplicateBO.duplicate((long) user.getUID());
        // 复制薪资账套的员工信息字段
        if (CollectionUtils.isNotEmpty(result.getSalarySobEmpFields())) {
            salarySobEmpFieldService.batchInsert(result.getSalarySobEmpFields());
        }
        // 复制薪资账套的薪资项目副本
        if (CollectionUtils.isNotEmpty(result.getSalarySobItems())) {
            salarySobItemService.batchInsert(result.getSalarySobItems());
        }

        // 复制薪资账套的回算薪资项目
        if (CollectionUtils.isNotEmpty(result.getSalarySobBackItems())) {
            getSalarySobBackItemService(user).batchInsert(result.getSalarySobBackItems());
        }


        // 复制薪资账套的薪资项目分类
        if (CollectionUtils.isNotEmpty(result.getSalarySobItemGroups())) {
            salarySobItemGroupService.batchInsert(result.getSalarySobItemGroups());
        }


        // 复制薪资账套的调薪计薪规则
        if (CollectionUtils.isNotEmpty(result.getSalaryAdjustmentRules())) {
            salarySobAdjustRuleService.batchInsert(result.getSalaryAdjustmentRules());
        }
        // 复制薪资账套的校验规则
        if (CollectionUtils.isNotEmpty(result.getSalarySobCheckRules())) {
            getSalarySobCheckRuleService(user).batchSave(result.getSalarySobCheckRules());
        }
        // 记录日志
        LoggerContext<SalarySobPO> loggerContext = new LoggerContext<>();
        loggerContext.setUser(user);
        loggerContext.setTargetId("" + result.getSalarySob().getId());
        loggerContext.setTargetName(result.getSalarySob().getName());
        loggerContext.setOperateType(OperateTypeEnum.UPDATE.getValue());
        loggerContext.setOperateTypeName(SalaryI18nUtil.getI18nLabel(98570, "复制薪资账套"));
        loggerContext.setOperatedesc(SalaryI18nUtil.getI18nLabel(98570, "复制薪资账套") + ": " + salarySobPO.getName());
        SalaryElogConfig.salarySobLoggerTemplate.write(loggerContext);
    }

    @Override
    public SalarySobCycleDTO getSalarySobCycle(Long id, YearMonth salaryMonth) {
        // 查询薪资账套
        SalarySobPO salarySobPO = getById(id);
        if (Objects.isNull(salarySobPO)) {
            throw new SalaryRunTimeException(SalaryI18nUtil.getI18nLabel(98379, "参数错误，薪资账套不存在或者已被删除"));
        }
        return SalarySobCycleBO.buildSalarySobCycle(salarySobPO, salaryMonth);
    }

    @Override
    public List<SalarySobPO> listByTaxAgentIds(List<Long> taxAgentIds) {
        if (CollectionUtils.isEmpty(taxAgentIds)) {
            return new ArrayList<>();
        }
        return getSalarySobMapper().listByTaxAgentIds(taxAgentIds);
    }

    @Override
    public List<SalarySobConfig> getConfig(Long taxAgentId) {
        List<SalarySobPO> salarySobPOS = getSalarySobMapper().listSome(SalarySobPO.builder().taxAgentId(taxAgentId).build());
        return salarySobPOS.stream().map(salarySobPO -> {
            Long salarySobId = salarySobPO.getId();
            List<SalarySobEmpFieldPO> salarySobEmpFieldPOS = getSalarySobEmpFieldService(user).listBySalarySobId(salarySobId);
            List<SalarySobItemGroupPO> salarySobItemGroupPOS = getSalarySobItemGroupService(user).getConfig(salarySobId);
            List<SalarySobItemPO> salarySobItemPOS = getSalarySobItemService(user).getConfig(salarySobId);
            List<SalarySobBackItemPO> salarySobBackItemPOS = getSalarySobBackItemService(user).getConfig(salarySobId);
            List<SalarySobAdjustRulePO> salarySobAdjustRulePOS = getSalarySobAdjustRuleService(user).listBySalarySobId(salarySobId);
            return SalarySobConfig.builder().salarySob(salarySobPO).salarySobEmpFields(salarySobEmpFieldPOS).salarySobItemGroups(salarySobItemGroupPOS).salarySobItems(salarySobItemPOS).salarySobBackItems(salarySobBackItemPOS).salarySobAdjustRules(salarySobAdjustRulePOS).build();
        }).collect(Collectors.toList());
    }

    @Override
    public List<UploadConfigResponse.Result> parseConfig(Long taxAgentId, List<SalarySobConfig> salarySobConfigs) {
        List<UploadConfigResponse.Result> results = new ArrayList<>();
        long uid = user.getUID();
        List<SalarySobPO> salarySobPOS = listByTaxAgentId(taxAgentId);
        Map<String, SalarySobPO> sobPOMap = SalaryEntityUtil.convert2Map(salarySobPOS, SalarySobPO::getName);

        List<SalaryItemPO> salaryItemPOList = getSalaryItemService(user).listAll();
        Map<String, SalaryItemPO> salaryItemNameMap = SalaryEntityUtil.convert2Map(salaryItemPOList, SalaryItemPO::getName);

        Optional.ofNullable(salarySobConfigs).orElse(new ArrayList<>()).forEach(salarySobConfig -> {
            SalarySobPO salarySobPO = salarySobConfig.getSalarySob();
            String sobPOName = salarySobPO.getName();

            UploadConfigResponse.Result result = UploadConfigResponse.Result.builder()
                    .message(String.format("账套:%s基础信息加载完毕", sobPOName))
                    .success(new ArrayList<>())
                    .warning(new ArrayList<>())
                    .error(new ArrayList<>())
                    .build();

            if (sobPOMap.containsKey(sobPOName)) {
                SalarySobPO oldSob = sobPOMap.get(sobPOName);
                salarySobPO.setId(oldSob.getId());
                salarySobPO.setCreator(uid);
                salarySobPO.setTaxAgentId(oldSob.getTaxAgentId());
                getSalarySobMapper().updateIgnoreNull(salarySobPO);
            } else {
                salarySobPO.setCreator(uid);
                salarySobPO.setTaxAgentId(taxAgentId);
                getSalarySobMapper().insertIgnoreNull(salarySobPO);
            }

            result.getSuccess().add(String.format("账套:%s基础信息加载成功", sobPOName));

            Long sobId = salarySobPO.getId();
            getSalarySobEmpFieldService(user).deleteBySalarySobIds(Collections.singletonList(sobId));
            List<SalarySobEmpFieldPO> salarySobEmpFields = salarySobConfig.getSalarySobEmpFields();
            if (CollectionUtil.isNotEmpty(salarySobEmpFields)) {
                for (SalarySobEmpFieldPO salarySobEmpField : salarySobEmpFields) {
                    salarySobEmpField.setSalarySobId(sobId);
                    salarySobEmpField.setCreator(uid);
                }
                getSalarySobEmpFieldService(user).batchSave(salarySobEmpFields);
            }
            result.getSuccess().add(String.format("账套:%s人员字段加载成功", sobPOName));

            getSalarySobItemService(user).deleteBySalarySobIds(Collections.singletonList(sobId));
            List<SalarySobItemPO> salarySobItems = salarySobConfig.getSalarySobItems();
            if (CollectionUtil.isNotEmpty(salarySobItems)) {
                salarySobItems.forEach(sobItem -> {
                    sobItem.setId(IdGenerator.generate());
                    sobItem.setSalarySobId(sobId);
                    sobItem.setCreator(uid);
                    Long formulaId = getSalaryFormulaService(user).add(sobItem.getFormula());
                    sobItem.setFormulaId(formulaId);

                    SalaryItemPO salaryItem = sobItem.getSalaryItem();
                    if (salaryItem != null) {
                        SalaryItemPO salaryItemPO = salaryItemNameMap.get(salaryItem.getName());
                        if (salaryItemPO != null) {
                            sobItem.setSalaryItemId(salaryItemPO.getId());
                        } else {
                            SalaryItemPO itemPO = getSalaryItemService(user).getItem(salaryItem);
                            if (itemPO != null) {
                                sobItem.setSalaryItemId(itemPO.getId());
                            } else {
                                result.getError().add(String.format("错误，账套:%s,薪资项目:%s,未找到对应薪资项目id", sobPOName, salaryItem.getName()));
                                log.error("item not found:{}", salaryItem);
                            }
                        }
                    }

                });
                getSalarySobItemService(user).batchSave(salarySobItems);
            }

            getSalarySobItemGroupService(user).deleteBySalarySobIds(Collections.singletonList(sobId));
            List<SalarySobItemGroupPO> salarySobItemGroups = salarySobConfig.getSalarySobItemGroups();
            if (CollectionUtil.isNotEmpty(salarySobItemGroups)) {
                for (SalarySobItemGroupPO salarySobItemGroup : salarySobItemGroups) {
                    long groupId = IdGenerator.generate();
                    salarySobItemGroup.setId(groupId);
                    salarySobItemGroup.setSalarySobId(sobId);
                    salarySobItemGroup.setCreator(uid);

                    List<SalarySobItemPO> sobItems = salarySobItemGroup.getSalarySobItems();
                    if (CollectionUtil.isNotEmpty(sobItems)) {
                        sobItems.forEach(sobItem -> {
                            sobItem.setId(IdGenerator.generate());
                            sobItem.setSalarySobId(sobId);
                            sobItem.setSalarySobItemGroupId(groupId);
                            sobItem.setCreator(uid);
                            Long formulaId = getSalaryFormulaService(user).add(sobItem.getFormula());
                            sobItem.setFormulaId(formulaId);

                            SalaryItemPO salaryItem = sobItem.getSalaryItem();
                            if (salaryItem != null) {
                                SalaryItemPO salaryItemPO = salaryItemNameMap.get(salaryItem.getName());
                                if (salaryItemPO != null) {
                                    sobItem.setSalaryItemId(salaryItemPO.getId());
                                } else {
                                    SalaryItemPO itemPO = getSalaryItemService(user).getItem(salaryItem);
                                    if (itemPO != null) {
                                        sobItem.setSalaryItemId(itemPO.getId());
                                    } else {
                                        result.getError().add(String.format("错误，账套:%s,薪资项目:%s,未找到对应薪资项目id", sobPOName, salaryItem.getName()));
                                        log.error("item not found:{}", salaryItem);
                                    }
                                }
                            }
                        });
                        getSalarySobItemService(user).batchSave(sobItems);
                    }
                }
                getSalarySobItemGroupService(user).batchSave(salarySobItemGroups);
            }
            result.getSuccess().add(String.format("账套:%s薪资项目加载成功", sobPOName));

            getSalarySobBackItemService(user).deleteBySalarySobIds(Collections.singletonList(sobId));
            List<SalarySobBackItemPO> salarySobBackItems = salarySobConfig.getSalarySobBackItems();
            if (CollectionUtil.isNotEmpty(salarySobBackItems)) {
                for (SalarySobBackItemPO salarySobBackItem : salarySobBackItems) {
                    salarySobBackItem.setId(IdGenerator.generate());
                    salarySobBackItem.setSalarySobId(sobId);
                    salarySobBackItem.setCreator(uid);

                    Long formulaId = getSalaryFormulaService(user).add(salarySobBackItem.getFormula());
                    salarySobBackItem.setFormulaId(formulaId);

                    SalaryItemPO salaryItem = salarySobBackItem.getSalaryItem();
                    if (salaryItem != null) {
                        SalaryItemPO salaryItemPO = salaryItemNameMap.get(salaryItem.getName());
                        if (salaryItemPO != null) {
                            salarySobBackItem.setSalaryItemId(salaryItemPO.getId());
                        } else {
                            SalaryItemPO itemPO = getSalaryItemService(user).getItem(salaryItem);
                            if (itemPO != null) {
                                salarySobBackItem.setSalaryItemId(itemPO.getId());
                            } else {
                                result.getError().add(String.format("错误，账套:%s,薪资项目:%s,未找到对应薪资项目id", sobPOName, salaryItem.getName()));
                                log.error("item not found:{}", salaryItem);
                            }
                        }
                    }
                }
                getSalarySobBackItemService(user).batchInsert(salarySobBackItems);
            }
            result.getSuccess().add(String.format("账套:%s回算加载成功", sobPOName));

            getSalarySobAdjustRuleService(user).deleteBySalarySobIds(Collections.singletonList(sobId));
            List<SalarySobAdjustRulePO> salarySobAdjustRules = salarySobConfig.getSalarySobAdjustRules();
            if (CollectionUtil.isNotEmpty(salarySobAdjustRules)) {
                for (SalarySobAdjustRulePO salarySobAdjustRule : salarySobAdjustRules) {
                    salarySobAdjustRule.setId(IdGenerator.generate());
                    salarySobAdjustRule.setSalarySobId(sobId);
                    salarySobAdjustRule.setCreator(uid);
                }
                getSalarySobAdjustRuleService(user).batchSave(salarySobAdjustRules);
            }
            result.getSuccess().add(String.format("账套:%s调薪计薪规则加载成功", sobPOName));

            results.add(result);
        });

        return results;
    }

    @Override
    public List<SalarySobPO> list(SalarySobQueryParam param) {
        SalarySobPO sobPO = SalarySobPO.builder().build();
        if (StrUtil.isNotBlank(param.getName())) {
            sobPO.setName(param.getName());
        }

        if (param.getTaxAgentId() != null) {
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().taxAgentId(param.getTaxAgentId()).build());
            sobPO.setIds(SalaryEntityUtil.properties(sobTaxLinkPOS, SobTaxLinkPO::getSobId));
        }
        return getSalarySobMapper().listSome(sobPO);
    }

    @Override
    public void handleHistory() {
        List<SalarySobPO> salarySobPOS = getSalarySobMapper().listAll();
        salarySobPOS.forEach(sobPO -> {
            List<SobTaxLinkPO> sobTaxLinkPOS = getSobTaxLinkMapper().listSome(SobTaxLinkPO.builder().sobId(sobPO.getId()).build());
            if(CollUtil.isEmpty(sobTaxLinkPOS)){
                SobTaxLinkPO taxLinkPO = SobTaxLinkPO.builder()
                        .id(IdGenerator.generate())
                        .taxAgentId(sobPO.getTaxAgentId())
                        .sobId(sobPO.getId())
                        .creator((long) user.getUID())
                        .createTime(new Date())
                        .updateTime(new Date())
                        .deleteType(0)
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .build();
                getSobTaxLinkMapper().insertIgnoreNull(taxLinkPO);
            }
        });
    }
}
